package com.common.utils;



import com.common.convert.Convert;

import java.util.Date;
import java.util.Random;

/**
 * 交易号生成规则
 * @author lihh
 * @since 2015/12/10
 */
public class GenerateNoUtil {
    private final static String ORDER_NO_PRE = "68";
    private final static String RECHARGE_NO_PRE = "RE";
    private final static String WITHDRAW_NO_PRE = "WD";
    /**
     * 生成订单号
     * @param memberId
     * @return
     */
    public static String genOrderNo(Integer memberId){
        return genNo(ORDER_NO_PRE, "yyMMdd", memberId);
    }

    /**
     * 生成充值订单号
     * @param memberId
     * @return
     */
    public static String genRechargeNo(Integer memberId){
        return genNo(RECHARGE_NO_PRE, "yyMMddHHmm", memberId);
    }

    /**
     * 生成体现订单号
     * @param memberId
     * @return
     */
    public static String genWithdrawNo(Integer memberId){
        return genNo(WITHDRAW_NO_PRE, "yyMMddHHmm", memberId);
    }

    private static String genNo(String pre, String dateFromat, Integer memberId){
        StringBuffer buffer = new StringBuffer(pre);
        buffer.append(DateUtil.toString(new Date(), dateFromat));
        Random random = new Random();
        buffer.append(random.nextInt(1000000));
        String mid = Convert.asString(memberId);
        if (mid.length() == 1){
            mid = "000" + mid;
        }else if (mid.length() == 2){
            mid = "00" + mid;
        }else if (mid.length() == 3){
            mid = "0" + mid;
        }
        if (mid.length() > 4){
            mid = mid.substring(mid.length() - 4);
        }
        buffer.append(mid);
        return buffer.toString();
    }
}
